local DIY = require "packages/diy_utility/diy_utility"

local haitu = fk.CreateSkill{
  name = "investiture__haitu",
}

Fk:loadTranslationTable{
  ["investiture__haitu"] = "醢兔",
  [":investiture__haitu"] = "你死亡时，可分配中央区内能恢复体力的牌。"
  .. "<br/><font color='grey'>耦：描述中包含“回复”就算！</font>",

  ["#investiture__haitu-ivk"] = "醢兔：你可以分配中央区内能恢复体力的牌（共%arg张）",
  ["#investiture__haitu-prompt"] = "醢兔：你可以分配中央区内能恢复体力的牌",

  ["$investiture__haitu1"] = "肉酱可饲狐，神魂早归封神台。",
  ["$investiture__haitu2"] = "剁骨成泥日，正是白兔跃月时！",
}

---@param room Room
---@return integer[]
local function getReavel(room)
  return table.filter(DIY.getCenterCards(room), function (id)
    if string.find(Fk:translate(":"..Fk:getCardById(id).name), "回复") then
      return true
    end
  end)
end

haitu:addAcquireEffect(function (self, player)
  player.room:addSkill("#CenterArea")
end)

haitu:addEffect(fk.Death, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(haitu.name, true, true) and player == target and #DIY.getCenterCards(player.room) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = getReavel(room)
    if #cards > 0 then
      return room:askToSkillInvoke(player, {
        skill_name = haitu.name,
        prompt = "#investiture__haitu-ivk:::" .. #cards
      })
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = getReavel(room)
    room:askToYiji(player, {
      targets = room:getOtherPlayers(player, false),
      expand_pile = cards,
      cards = cards,
      max_num = 999,
      min_num = 0,
      skill_name = haitu.name,
      prompt = "#investiture__haitu-prompt",
      skip = false,
    })
  end,
})

return haitu